package com.wyj.longrangeservice;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.wyj.longrangeservice.Bean.ActionBean;
import com.wyj.longrangeservice.Util.WebSocketUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author : 泥菩萨
 * time   : 2023/04/14
 */
public class ControlActivity extends AppCompatActivity {

    public static void startControlActivity(Context context,String name){

        Intent intent = new Intent(context,ControlActivity.class);
        intent.putExtra("url","ws://81.71.4.249:9999/rtsp_websocket?name=" + name + "cam");
//        intent.putExtra("url","ws://192.168.8.9:9999/rtsp_websocket?name=" + name + "cam");
        context.startActivity(intent);
    }

    private ImageView img;

    private TextView long_up_text;

    private Float width,height;

    private boolean isLongUp = false;

    private List<Byte> imgData = new ArrayList<>();

    private int byteNax = 1024;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_control);

        img = findViewById(R.id.img);

        long_up_text = findViewById(R.id.long_up_text);

        WebSocketUtil.getWebSocketUtil().setmCallback(new WebSocketUtil.Callback() {
            @Override
            public void getData(byte[] message) {

                try {

                    for (byte data:message)imgData.add(data);

                    if (message.length != byteNax){

                        byte[] imgdata = new byte[imgData.size()];
                        for (int i=0;i<imgData.size();i++)imgdata[i]=imgData.get(i);

                        imgData.clear();

                        Bitmap bitmap = BitmapFactory.decodeByteArray(imgdata,0,imgdata.length);

                        if (bitmap==null)return;

                        if (height==null || width==null){

                            height = Float.valueOf(bitmap.getHeight());
                            width = Float.valueOf(bitmap.getWidth());

                            ControlActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    setImgSize();
                                }
                            });


                        }else {

                            ControlActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                    img.setImageBitmap(bitmap);
                                }
                            });
                        }

                    }

                } catch (Exception e) {

                    e.printStackTrace();
                }

            }

            @Override
            public void getData(String data) {

            }

            @Override
            public void msg(String data) {

            }
        });

        WebSocketUtil
                .getWebSocketUtil()
                .connect(getIntent().getStringExtra("url"));

        findViewById(R.id.home_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ActionBean mActionBean = new ActionBean();
                mActionBean.setAppAction("home");
                WebSocketUtil.getWebSocketUtil().send(JSON.toJSONString(mActionBean));
            }
        });

        findViewById(R.id.back_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ActionBean mActionBean = new ActionBean();
                mActionBean.setAppAction("back");
                WebSocketUtil.getWebSocketUtil().send(JSON.toJSONString(mActionBean));
            }
        });

        long_up_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                isLongUp = !isLongUp;
                long_up_text.setText(isLongUp?"长按模式":"点击模式");
            }
        });
    }

    private void setImgSize(){

        if (height==null || width==null)return;

        float scale = height/width;
        float locaScale = ((float) getResources().getDisplayMetrics().heightPixels)/((float) getResources().getDisplayMetrics().widthPixels);

        int imgWidth,imgHeight;

        if (locaScale>=scale){

            imgWidth = getResources().getDisplayMetrics().widthPixels;
            imgHeight = (int) (imgWidth * scale);

        }else {

            imgHeight = getResources().getDisplayMetrics().heightPixels;
            imgWidth = (int) (imgHeight/scale);
        }

        ViewGroup.LayoutParams mLayoutParams = img.getLayoutParams();
        mLayoutParams.height = imgHeight;
        mLayoutParams.width = imgWidth;
        img.setLayoutParams(mLayoutParams);

        img.setOnTouchListener(new View.OnTouchListener() {

            float x1,y1,x2,y2;
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                switch (motionEvent.getAction()){

                    case MotionEvent.ACTION_DOWN:

                        x1 = motionEvent.getX();
                        y1 = motionEvent.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:

                        x2 = motionEvent.getX();
                        y2 = motionEvent.getY();
                        break;

                    case MotionEvent.ACTION_UP:

                        ActionBean mActionBean = new ActionBean();
                        mActionBean.setX1(x1/((float)view.getWidth()));
                        mActionBean.setY1(y1/((float)view.getHeight()));
                        mActionBean.setX2(x2/((float)view.getWidth()));
                        mActionBean.setY2(y2/((float)view.getHeight()));

                        if (Math.abs(x1-x2)>20 || Math.abs(y1-y2)>20){

                            mActionBean.setAppAction("hd");

                        }else {

                            mActionBean.setAppAction(isLongUp?"ca":"dj");
                        }

                        WebSocketUtil.getWebSocketUtil().send(JSON.toJSONString(mActionBean));

                        break;
                }

                return true;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        WebSocketUtil.getWebSocketUtil().cancel();
    }
}
